package com.mall.serviceImpl;

import com.mall.bean.SystemPermission;
import com.mall.mapper.SystemPermissionMapper;
import com.mall.service.SystemPermissionService;
import com.mall.vo.ChildrenMenuVo;
import com.mall.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @PageName com.mall.service
 * @Description 权限service实现类
 * @Author 何全裕
 * @Create 2017-09-03 22:51
 * @Version 1.0
 **/
@Service("systemPermission")
public class SystemPermissionServiceImpl implements SystemPermissionService {

    @Autowired
    private SystemPermissionMapper systemPermissionMapper;

    public List<SystemPermission> getPermissionList() {
        return systemPermissionMapper.getPermissionList();
    }

    public List<SystemPermission> getPermissionListByType() {
        return systemPermissionMapper.getPermissionListByType();
    }

    public SystemPermission findPermission(SystemPermission systemPermission) {
        return null;
    }

    public SystemPermission findPermissionById(String id) {
        return systemPermissionMapper.findPermissionById(id);
    }

    public void insertPermission(SystemPermission systemPermission) {

    }

    public void insertPermissionSelective(SystemPermission systemPermission) {
        systemPermissionMapper.insertPermissionSelective(systemPermission);
    }


    public void updatePermission(SystemPermission systemPermission) {
        systemPermissionMapper.updatePermission(systemPermission);
    }

    public void deletePermission(String id) {
        systemPermissionMapper.deletePermission(id);
    }

    public void deleteAllPermission(String[] ids) {
        systemPermissionMapper.deleteAllPermission(ids);
    }

    public List<MenuVo> getUserPermission(String user_id) {
        List<SystemPermission> userPermission = systemPermissionMapper.getUserPermission(user_id);
        List<SystemPermission> paren = new ArrayList<SystemPermission>();
        for(SystemPermission sp : userPermission){
            if(sp.getParentid() == 1){ //一级菜单
                paren.add(sp);
            }
        }
        List<MenuVo> voList = new ArrayList<MenuVo>();
        for(SystemPermission parent : paren){
            MenuVo mvo = new MenuVo();
            mvo.setTitle(parent.getName());
            mvo.setIcon(parent.getParentids());
            mvo.setSpread(false);
            List<ChildrenMenuVo> chlirden = new ArrayList<ChildrenMenuVo>();
            for(SystemPermission sp : userPermission){
                if(sp.getParentid() == parent.getId()){  //属于
                    ChildrenMenuVo ChildrenMenuVo = new ChildrenMenuVo();
                    ChildrenMenuVo.setTitle(sp.getName());
                    ChildrenMenuVo.setIcon(sp.getParentids());
                    ChildrenMenuVo.setHref(sp.getUrl());
                    chlirden.add(ChildrenMenuVo);
                }
            }
            mvo.setChildren(chlirden);
            voList.add(mvo);
        }
        return voList;
    }


}
